package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.TSpecDao;
import com.shop.shoppingmall.entity.TSpec;
import com.shop.shoppingmall.service.TSkuService;
import com.shop.shoppingmall.service.TSpecService;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (TSpec)表服务实现类
 *
 * @author makejava
 * @since 2020-09-23 16:16:54
 */
@Service("tSpecService")
@Transactional
public class TSpecServiceImpl implements TSpecService {
    @Resource
    private TSpecDao tSpecDao;
    @Resource
    private TSkuService tSkuService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TSpec queryById(Integer id) {
        return this.tSpecDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param tSpec 实例对象
     * @return 实例对象
     */
    @Override
    public TSpec insert(TSpec tSpec) {
        this.tSpecDao.insert(tSpec);
        return tSpec;
    }

    /**
     * 修改数据
     *
     * @param tSpec 实例对象
     * @return 实例对象
     */
    @Override
    public TSpec update(TSpec tSpec) {
        this.tSpecDao.update(tSpec);
        return this.queryById(tSpec.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tSpecDao.deleteById(id) > 0;
    }


    @Override
    public List<TSpec> findSpecListBySkuIds(List<String> skuList) throws Exception {
        return tSpecDao.findSpecListBySkuIds(skuList);
    }

    @Override
    public Map<String,List<TSpec>> findAttrListByGoodsId(String goodsId) {
        Map<String,List<TSpec>> map= new HashMap<>();
        try {
            List<String>  skuList=tSkuService.findSkuIdListByGoodsId(goodsId);
            List<TSpec>  specPOList=findSpecListBySkuIds(skuList);
            map=new HashMap<>(specPOList.size());
            for(TSpec  t:specPOList){
                List<TSpec> tempList=tSpecDao.findAttrListByPspecIdAndSkuIds(t.getId(),skuList);
                map.put(t.getSpecName(),tempList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }



    /**
     * 添加商品属性规格
     * @param specName
     * @param isParent
     * @return
     */
    @Override
    public Result addTsSpect(Integer id,String specName, Integer isParent) {
        TSpec tSpec = tSpecDao.selectSpec(specName, isParent);
        if(tSpec != null){
            return Result.error("已有该属性或规格，请重新选择");
        }
        TSpec tSpec1 = new TSpec();
        if(isParent == 0){
            String pSpectId = String.valueOf(id);
            tSpec1.setPSpecId(pSpectId);
            tSpec1.setSpecName(specName);
            tSpec1.setIsParent(isParent);
            tSpecDao.insert(tSpec1);
            return Result.ok("添加成功！");
        }else {
            String pSpectId = String.valueOf(id);
            tSpec1.setPSpecId(pSpectId);
            tSpec1.setSpecName(specName);
            tSpec1.setIsParent(isParent);
            tSpecDao.insert(tSpec1);
            return Result.ok("添加成功!");
        }
    }

    /**
     * 查询所有的规格属性
     * @return
     */
    @Override
    public Result selectAll() {
        List<TSpec> tSpecs = tSpecDao.selectAll();
        if(tSpecs == null){
            return Result.error("暂无属性规格信息，请添加");
        }
        return Result.ok(tSpecs);
    }

}